package com.glink.manage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author xum
 * @since 2024/6/21
 **/
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor myThreadPoolExecutor() {
        //核心线程数
        int corePooSize = 50;
        // 最大核心线程数
        int maximumPoolSize = 50;
        // 无任务时线程最大存活时间
        int keepAliveTime = 10;
        // 时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        // 队列规则
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1000);
        // 线程池工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        // 抛出异常类型
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
        // 创建线程池
        return new ThreadPoolExecutor(
                corePooSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler);
    }
}
